$(document).ready(function(){
	var VAL ={
	        NORMAL     : "0000000"  ,
	        EVT_USER   : $("#userId").text(),
	        T_FBPBISOPE	 : 'FBPBISOPE',
	        T_FBPBISPTH  : 'FBPBISPTH',
	        T_FIPINQBOX  : 'FIPINQBOX',
	        T_FSPSKIPOPE : 'FSPSKIPOPE',
	        DISABLED_ATTR : {
			   'disabled' : true
		    },
		    ENABLED_ATTR : {
			   'disabled' : false
		    }
	    };
	var domObj = {
			prdInfoListDiv : $("#prdInfoListDiv"),
			prdInfoListGrd : $("#prdInfoListGrd"),
			prdInfoListPg : "#prdInfoListPg"
	};
	var btnQuery = {
		       query  	 : $("#query_btn"),
		       skip  	 : $("#skip_btn")
		    };
	var controlsQuery = {
			opeIdInp    : $("#opeIdInp"),
			boxIDTxt 	: $("#boxIDTxt"),
			skipOpeIDTxt :$("#skipOpeIDTxt"),
			boxIdSpan	: $("#boxIdSpan"),
			boxStatSpan:  $("#boxStatSpan"),
			prdQtySpan:   $("#prdQtySpan"),
			pathSpan:     $("#pathSpan"),
			pathVerSpan:  $("#pathVerSpan"),
			crOpeIdSpan:  $("#crOpeIdSpan"),
			cusOrdIdSpan: $("#cusOrdIdSpan"),
			mdlIdSpan:    $("#mdlIdSpan")
	};
	
	var iniGridInfo = function(){
		var prdInfoCM = [
        {name: 'slot_no',   		index: 'slot_no',   		label: PPBOX_SLOT_NO_TAG    , width: 80},
        {name: 'prd_seq_id', 			index: 'prd_seq_id',			label: SHEET_ID_TAG    , 		width: 120},
        {name: 'so_id_fk',		index: 'so_id_fk', 		label: SO_NO_TAG,			width: 80},
        {name: 'mdl_id_fk',    	index: 'mdl_id_fk',   	label: PRODUCT_ID_TAG  , 	width: 80},
        {name: 'path_id_fk',     	index: 'path_id_fk',   	label: PATH_ID_TAG  , 	width: 80},
        {name: 'ope_id_fk',     	index: 'ope_id_fk',   	label: CR_OPE_ID_TAG  , 	width: 80}
        ];
		domObj.prdInfoListGrd.jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            height:425,
            width:domObj.prdInfoListDiv.width(),
            shrinkToFit:true,
            scroll:true,
            rownumWidth : true,
            resizable : true,
            rowNum:40,
            loadonce:true,
            fixed:true,
            viewrecords:true,
//            multiselect : true,
            pager : domObj.prdInfoListPg,
            colModel: prdInfoCM,
            onSelectRow: function(id){
            	
            	}
        });
		};
	var iniButtonAction = function(){
		btnQuery.query.click(function(){
			btnFunc.query_func();
		});
		btnQuery.skip.click(function(){
			btnFunc.skip_func();
		});
		controlsQuery.boxIDTxt.bind('keyup',function(event){
		    if(event.keyCode==13){
		    	btnFunc.query_func();
		      }
		    });
	};
	var btnFunc = {
			setOpeSelectDate : function(dataCnt, arr, selVal, selVal2, selTxt, queryObj,emptySel){
	            var i, realCnt;

	            queryObj.empty();
	            realCnt = parseInt( dataCnt, 10);
	            if (emptySel) {
						queryObj.append("<option value=''></option>");
				}
	            if( realCnt > 0 ){
	                for( i = 0; i < realCnt; i++ ){
	                        queryObj.append("<option value="+ arr[i][selVal] + "@" + arr[i][selVal2] +">"+ arr[i][selTxt] +"</option>");
	                }
	            }
	            queryObj.select2({
	    	    	theme : "bootstrap"
	    	    });
	        },
			setPathInfo : function(path_id,path_ver){
			    var iaryB = {};
			    iaryB.path_id_fk  = path_id;
			    iaryB.path_ver_fk = path_ver;
				var inObj = {
						trx_id	   : VAL.T_FBPBISPTH,
						action_flg : "I",
                    	iaryB      : [iaryB]
					};
					var outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code == VAL.NORMAL) {
						btnFunc.setOpeSelectDate(outObj.tbl_cnt_b, outObj.oaryB, "cr_ope_id_fk", "cr_ope_ver_fk", "cr_ope_no", controlsQuery.skipOpeIDTxt,true);
					}
			
			},
			query_func :function(){
				var boxId = controlsQuery.boxIDTxt.val();
				if(!boxId){
					showErrorDialog("","请输入卡匣代码");
					return;
				}
				
				var inTrxObj ={
		       	         trx_id     : VAL.T_FIPINQBOX,
		       	         action_flg : 'I'        ,   
		       	         box_id 	: boxId
		       	      };
		       	      var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
		       	      if(  outTrxObj.rtn_code == VAL.NORMAL ) {
			       	         setGridInfo(outTrxObj.oaryC,"#prdInfoListGrd");
			       	         clearSpanFnc();
			       	         controlsQuery.boxIdSpan.html(outTrxObj.box_id);	 
			       	         controlsQuery.boxStatSpan.html(outTrxObj.box_stat);   
			       	         controlsQuery.prdQtySpan.html(outTrxObj.prd_qty);    
			       	         controlsQuery.pathSpan.html(outTrxObj.nx_path_id_fk);      
			       	         controlsQuery.pathVerSpan.html(outTrxObj.nx_path_ver_fk);   
			       	         controlsQuery.crOpeIdSpan.html(outTrxObj.nx_ope_no_fk);   
			       	         controlsQuery.cusOrdIdSpan.html(outTrxObj.so_id_fk);  
			       	         controlsQuery.mdlIdSpan.html(outTrxObj.mdl_id_fk);  
						     if(!outTrxObj.nx_path_id_fk){
								 	return;
							 }
			       	         btnFunc.setPathInfo(outTrxObj.nx_path_id_fk,outTrxObj.nx_path_ver_fk);
		       	      }
			},
			skip_func : function(){
				var box_stat = controlsQuery.boxStatSpan.html();
				var crOpeId = controlsQuery.crOpeIdSpan.html();
				var opeIdInp = controlsQuery.opeIdInp.val();
				if(!opeIdInp){
					showErrorDialog("","请输入站点！")
					return;
				} else if (opeIdInp != crOpeId) {
					showErrorDialog("","输入的站点要和卡匣的当前站点一致！");
					return;
				}
				if (box_stat != "WAIT") {
					showErrorDialog("","["+box_stat+"]状态的箱子不能跳站！");
					return;
				}
				var skipOpeId = controlsQuery.skipOpeIDTxt.find("option:selected").val();
		       	 if(skipOpeId){
		       		 var ope_id = skipOpeId.split("@")[0];
		       		 var ope_ver = skipOpeId.split("@")[1];
		       		 var inTrxObj ={
		       				 trx_id     : VAL.T_FBPBISOPE,
		       				 action_flg : 'Q'        ,   
		       				 iary		: [{
		       					 ope_id 	: ope_id,
			       				 ope_ver    : ope_ver
		       				 }]
		       				
		       		 };
		       		 var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
		       		 if(  outTrxObj.rtn_code == VAL.NORMAL ) {
		       			 if (outTrxObj.oary[0].tool_run_mode === "UPK") {
		       				showErrorDialog("","不能转入解封的站点");
							return;
						}
		       		 }
		       	 }
		       	var ope_no = controlsQuery.skipOpeIDTxt.find("option:selected").text(); 
		       	var box_id = controlsQuery.boxIdSpan.html();
				var rows = domObj.prdInfoListGrd.jqGrid("getRowData");
				var path_id = controlsQuery.pathSpan.html();
				var path_ver = controlsQuery.pathVerSpan.html();
				var rows = domObj.prdInfoListGrd.jqGrid("getRowData");
				var iaryB = new Array();
				var iaryInt;
				for (var int = 0; int < rows.length; int++) {
						iaryInt = {prd_seq_id :rows[int].prd_seq_id};
						iaryB[int] = iaryInt
					}
				
				if (!skipOpeId) {
					showErrorDialog("","转入站点不能为空");
					return;
				}
				if (!rows) {
					showErrorDialog("","玻璃信息列表不能为空");
					return;
				}
				var inObj = {
						trx_id : VAL.T_FSPSKIPOPE,
						action_flg : 'J',
						evt_usr : 'C1602020',
						iary :[{
							box_id : box_id,
							nx_ope_id_fk : ope_id,
							nx_ope_ver_fk: ope_ver,
							nx_ope_no_fk : ope_no,
							nx_path_id_fk: path_id,
							nx_path_ver_fk : path_ver
						}],
						iaryB : iaryB
					};
				var outObj = comTrxSubSendPostJson(inObj);
				if (outObj.rtn_code == VAL.NORMAL) {
					showSuccessDialog("跳站成功");
					btnFunc.query_func();
				}
			}
			
	};

	function clearSpanFnc() {
        controlsQuery.boxIdSpan.html("");
        controlsQuery.boxStatSpan.html("");
        controlsQuery.prdQtySpan.html("");
        controlsQuery.pathSpan.html("");
        controlsQuery.pathVerSpan.html("");
        controlsQuery.crOpeIdSpan.html("");
        controlsQuery.cusOrdIdSpan.html("");
        controlsQuery.mdlIdSpan.html("");
    }
	
	var initializationFunc = function(){
		$("form").submit(function(){
            return false;
        });
		iniGridInfo();
		iniButtonAction();
		$("select").empty();
//		iniContorlData();
		clearSpanFnc();
	};
	initializationFunc();
    //表格自适应
    function resizeFnc(){
        var offsetBottom, divWidth;
        divWidth = $("#prdInfoListDiv").width();
        offsetBottom = $(window).height() - $("#prdInfoListDiv").offset().top;
        // offsetBottom = $("#glassListDiv").offset().top;
        $("#prdInfoListDiv").height(offsetBottom * 0.95);
        $("#prdInfoListGrd").setGridWidth(divWidth * 0.99);
        $("#prdInfoListGrd").setGridHeight(offsetBottom * 0.95 - 51);
    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});
